package com.allwees.bs.module.scm.req.query;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.allwees.bs.core.modelbase.req.QueryReq;
import com.allwees.bs.module.core.req.BranchUserQuery;
import com.allwees.bs.module.scm.entity.ScmPickOrderSku;
import com.allwees.core.common.constant.EStatus;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * scm 待分配订单 : sku 维度
 *
 * @author mouhn
 * @since 2020-1-2 10:28
 */
@Data
public class ScmPickOrderSkuQueryReq extends QueryReq<ScmPickOrderSku> implements BranchUserQuery<ScmPickOrderSku> {

    private EScmPickOrderSkuQueryStatus status;

    private Date shippingAtFrom;
    private Date shippingAtTo;

    @Override
    public void buildQuery(QueryWrapper<ScmPickOrderSku> queryWrapper) {
        queryWrapper.and(StringUtils.isNotBlank(q), wrapper -> {
            wrapper.eq("uuid", q)
                    .or().eq("sku_no", q)
                    .or().eq("sku_custom_no", q)
                    .or().eq("batch_no", q)
                    .or().eq("order_no", q)
                    .or().eq("order_uuid", q);
        });
        buildBranchUserQuery(queryWrapper)
                .in("status", status.entityStatus())
                .ge(shippingAtFrom != null, "shipping_at", shippingAtFrom)
                .le(shippingAtTo != null, "shipping_at", shippingAtTo);
    }


    private enum EScmPickOrderSkuQueryStatus {
        // 所有、拣货中、缺货
        ALL {
            @Override
            List<Integer> entityStatus() {
                return Arrays.asList(
                        EStatus.NORMAL.value(),
                        EStatus.SELLOUT.value(),
                        EStatus.USED.value(),
                        EStatus.SCM_TIMEOUT.value(),
                        EStatus.SCM_TIMEOUT_REFUND.value()
                );
            }
        },
        PICKING {
            @Override
            List<Integer> entityStatus() {
                return Arrays.asList(EStatus.NORMAL.value());
            }
        },
        SELLOUT {
            @Override
            List<Integer> entityStatus() {
                return Arrays.asList(EStatus.SELLOUT.value());
            }
        };

        abstract List<Integer> entityStatus();
    }
}
